----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 4201 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 240
SPELL.m_sName = "Cleave"
SPELL.m_sToolTip = [[You swing your weapon
					in a sweeping arc, 
					damaging your target and two 
					nearby enemies.]]
SPELL.m_sIcon = "devin/blackbetty/spells/warrior/cleave.png"
SPELL.m_bRequiresTarget = true
SPELL.m_iManaCost = 0
SPELL.m_iCoolDown = 2
SPELL.m_iSpellType = SPELLTYPE_MAA
SPELL.m_bRecastWhileCasting = false

---------------------------------------------------------------------------------------

local range = 100

SPELL.Modifiers.testmod = {	 
							name = "Test Cleave Level 1", 
							desc = "Performs an exact copy of the current spell on two random enemies within 5 yards of the target."
							}
							
function SPELL.Modifiers.testmod:func(s, pl, target)
	local closestTargets = EnemiesInRange(pl, target, 240)
	local numTargets = 2
	local curTargets = 0
	for k, v in pairs(closestTargets) do
		if(v != nil)then
			s:Effect(pl, v, 0.5);
			curTargets = curTargets + 1
			print(curTargets)
		end
		if(curTargets == numTargets)then
			break
		end
	end	
end

function SPELL:Cast( pl, target )
	if(target && target:IsValid() && target != pl ) then
		pl.InSpellAnimation = true
		pl:DoAnimationEvent(ACT_HL2MP_GESTURE_RANGE_ATTACK_MELEE2)
		pl:EmitSound("devin/blackbetty/weapons/swing/swing_hit.wav", 100, 100+math.random(-10,10))
			timer.Simple(0.25, function()
				UpdatePlayerResource(pl, 20, true)
				self:Effect(pl, target, 1)
				self:ApplySpellMods(self, pl, target)
			end)
		timer.Simple(0.4, function()
			pl.InSpellAnimation = false
			pl:SetNWBool("Casting", false)
			pl:SetNWBool("Casted", false)
		end)
	end
end

function SPELL:Effect(pl, target, damagemod)
	local weapon = pl:GetWeapon("weapon_all");
	if(target and pl:GetPos():Distance(target:GetPos()) < self.m_iRange)then
		--pl:EmitSound("devin/blackbetty/weapons/Swipe.wav", 160, 80)\
		UpdatePlayerResource(pl, 5, true)
		if(SERVER)then
			if(target and target:EntIndex() != pl:EntIndex() and target:IsPlayer() or target:IsNPC() or target:GetClass() == "npc_rpgdm" and target:Health() > 0)then
				self:DealDamage((weapon.MinWeaponDamage*2)*damagemod, (weapon.MaxWeaponDamage*2)*damagemod, pl, target, DMG_GENERIC)
				target:EmitSound("devin/blackbetty/weapons/slam/hit_1.wav", 140, 100)
				ParticleEffect( "pummel_hit", target:GetPos() + Vector(0, 0, 46) + (target:GetAngles():Forward()*6), Angle(0,0,0), pl )
				ParticleEffect( "player_blood", target:GetPos() + Vector(0, 0, 46) + (target:GetAngles():Forward()*6), Angle(0,0,0), pl )
				target:SetVelocity(((pl:GetPos()- Vector(0, 0, 40)) - target:GetPos())*-2)
			end
		end
	end
end



